package zhihui.mobile.presenter.myself;

import android.content.Context;

import com.zgm.utils.SharedPreferencesUtis;

import org.json.JSONObject;

import zhihui.mobile.config.Config;
import zhihui.mobile.interfaces.net.IHttpCallBackListener;
import zhihui.mobile.manager.user.UserManager;
import zhihui.mobile.ui.IView;
import zhihui.mobile.ui.myself.IUpdateSexContract;
import zhihui.mobile.ui.myself.IUpdateSexContract.IUpdateSexPresenter;
import zhihui.mobile.ui.utils.SharedPreferenceUtils;

/**
 * Created by 张公铭 on 2017/9/24 0024.
 */

public class UpdateSexPresenter implements IUpdateSexPresenter
{
    private Context mContext;
    private IUpdateSexContract.IUpdateSexView sexView;

    @Override
    public void init(IView view, Context context)
    {
        mContext = context;
        sexView = (IUpdateSexContract.IUpdateSexView) view;
    }

    @Override
    public void onResume()
    {

    }

    @Override
    public void onStart()
    {

    }

    @Override
    public void onPause()
    {

    }

    @Override
    public void onStop()
    {

    }

    @Override
    public void onDestroy()
    {

    }

    @Override
    public void updateSex(final int sex)
    {
        sexView.showLoading();
        UserManager.getInstance().updateUserInfo(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                sexView.hindLoading();
                sexView.showMessage("修改成功");
                UserManager.getInstance().getCurUser().setSex(sex);
                SharedPreferencesUtis.saveSharedPreference(mContext, Config.USER_DATA_SAVE, Config.USER_DATA_SAVE, UserManager.getInstance().getCurUser());
            }

            @Override
            public void failed(String message)
            {
                sexView.hindLoading();
                sexView.showMessage(message);
            }
        },UserManager.getInstance().getCurUser().getName(), sex);
    }
}
